package cn.fl.spdealer.service.impl;

import java.util.List;
import java.util.Map;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spdealer.service.ISplGroupService;
import cn.fl.spdealer.transform.SplGroupVOTransform;
import cn.fl.spdealer.vo.SplGroupVO;
import sp.dealer.facade.dealer.api.ISplGroupApiService;
import sp.dealer.facade.dealer.condition.SplGroupQC;
import sp.dealer.facade.dealer.dto.SplGroupDTO;

/**
 * @Description: 经销商集团划分（包括 经销商 和挂靠公司） 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SplGroupServiceImpl implements ISplGroupService {

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ISplGroupApiService splGroupApiService;

	@Override
	public SplGroupVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		//TODO 业务逻辑
		SplGroupDTO dto = this.splGroupApiService.selectById(id);
		return SplGroupVOTransform.toVO(dto);
	}

	@Override
	public Long add(SplGroupVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增对象不能为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		return this.splGroupApiService.add(SplGroupVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newWebLogger("删除主键参数集合不能为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		};
		
		return this.splGroupApiService.removeByIds(ids);
    }

	@Override
	public boolean modify(SplGroupVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改对象参数不能为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		}
		return this.splGroupApiService.modify(SplGroupVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		return this.splGroupApiService.searchListPage(page);
	}

	@Override
	public List<SplGroupVO> searchList(SplGroupQC condition) throws BaseException {
		// TODO Auto-generated method stub
		if(condition == null){
			return null ;
		}
		List<SplGroupDTO> list = this.splGroupApiService.searchList(condition);
		
		return SplGroupVOTransform.toVOList(list);
	}

	@Override
	public int batchAdd(List<SplGroupVO> list) throws BaseException {
		// TODO Auto-generated method stub
		if(CollectionUtils.isEmpty(list)){
			return 0;
		}
		int i = this.splGroupApiService.batchAdd(SplGroupVOTransform.toDTOList(list));
		return i;
	}

	/**
	 * 根据条件修改集团信息
	 *
	 * @param splGroupQC
	 * @param mdfMap
	 */
	@Override
	public boolean modifySplGroupVOByQC(SplGroupQC splGroupQC, Map<String, Object> mdfMap) throws BaseException {
		if (null == splGroupQC) {
			BaseLogUtils.newWebLogger("经销商集团信息QC为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}
		if (CollectionUtils.isMapEmpty(mdfMap)) {
			BaseLogUtils.newWebLogger("修改经销商集团信息内容为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}
		try {
			this.splGroupApiService.cancelMember(splGroupQC,mdfMap);
			return true ;
		}catch (BaseException be){
			BaseLogUtils.newWebLogger("修改经销商集团信息内容为空,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(be)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}catch (Exception e){
			BaseLogUtils.newWebLogger("修改经销商集团信息内容为空,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}
	}

	@Override
	public boolean addMember(Long groupId,Long memberId,Long oldGroupId,Integer isAddAll,Long usrId) throws BaseException {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(memberId)){
			BaseLogUtils.newWebLogger("成员id不能为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}
		if(StringUtils.isEmpty(groupId)){
			BaseLogUtils.newWebLogger("集团id不能为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
		}
		
		return this.splGroupApiService.addMember(groupId, memberId, oldGroupId, isAddAll, usrId);
	}

	@Override
	public int seachSplGroupCountByQC(SplGroupQC condition) throws BaseException {
		// TODO Auto-generated method stub
		return this.splGroupApiService.seachSplGroupCountByQC(condition);
	}

}
